package com.workManager.controller;

import com.github.pagehelper.PageInfo;
import com.wordnik.swagger.annotations.ApiOperation;
import com.workManager.base.BaseController;
import com.workManager.pojo.TbProcurement;
import com.workManager.service.ProcurementService;
import com.workManager.util.WebUtil;
import com.workManager.vo.ProcurementVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by worso on 2017/3/21 0021.
 */
@Controller
@RequestMapping("/procurement")
public class ProcurementController extends BaseController{


    @Autowired
    private ProcurementService procurementService;

    @ApiOperation(value ="采购添加",notes = "采购添加")
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody TbProcurement procurement){
        ModelMap modelMap = new ModelMap();
        procurementService.add(procurement);
        return  setSuccessModelMap(modelMap,"保存成功");
    }

    @ApiOperation(value ="采购删除",notes = "采购删除")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Object delete(@RequestParam(value = "Ids[]") List<String> Ids){
        ModelMap modelMap = new ModelMap();
        procurementService.delete(Ids);
        return  setSuccessModelMap(modelMap,"删除成功");
    }


    @ApiOperation(value ="采购更新",notes = "采购更新")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(@RequestBody TbProcurement procurement){
        ModelMap modelMap = new ModelMap();
        procurementService.update(procurement);
        return  setSuccessModelMap(modelMap,"保存成功");
    }


    @ApiOperation(value ="采购查询",notes = "采购查询")
    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public Object query(
                        @RequestParam(value = "pageNumber",required = false) String pageNumber,
                        @RequestParam(value="pageSize", required = false) String pageSize,
                        @RequestParam(value="procurementId", required = false) String procurementId,
                        @RequestParam(value="projectCode", required = false) String projectCode,
                        @RequestParam(value="projectName", required = false) String projectName,
                        @RequestParam(value="purchaseDateFrom", required = false) String purchaseDateFrom,
                        @RequestParam(value="procurementType", required = false) String procurementType,
                        @RequestParam(value="purchaseDateTo", required = false) String purchaseDateTo,
                        @RequestParam(value="paymentDateFrom", required = false) String paymentDateFrom,
                        @RequestParam(value="paymentDateTo", required = false) String paymentDateTo,
                        @RequestParam(value = "loginCode", required = false) String loginCode
    ){
        ModelMap modelMap = new ModelMap();
        Map<String,Object> map = new HashMap<String, Object>();
        Integer pagenum = pageNumber==null?1:Integer.parseInt(pageNumber);
        Integer pagesize = pageSize==null?10:Integer.parseInt(pageSize);
        map.put("loginCode", loginCode);
        map.put("pagenum",pagenum);
        map.put("pagesize",pagesize);
        map.put("procurementId",procurementId);
        map.put("projectCode",projectCode);
        map.put("projectName",projectName);
        map.put("purchaseDateFrom",purchaseDateFrom);
        map.put("purchaseDateTo",purchaseDateTo);
        map.put("paymentDateFrom",paymentDateFrom);
        map.put("paymentDateTo",paymentDateTo);
        map.put("procurementType",procurementType);
        PageInfo<ProcurementVo> procurementList = procurementService.query(map);
        Map<String,Object> dataMap = new HashMap<String, Object>();
        dataMap.put("total",procurementList.getTotal());
        dataMap.put("rows",procurementList.getList());
        return  setSuccessModelMap(modelMap,dataMap);
    }
}
